﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Weight.Base
{
    public class Colors
    {
        [DllImport("user32.dll")]
        private static extern IntPtr GetDC(IntPtr hwnd);

        [DllImport("gdi32.dll")]
        private static extern int GetPixel(IntPtr hdc, Point p);

        [DllImport("user32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
        private static extern int GetDC(int hwnd);

        [DllImport("gdi32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
        private static extern int GetPixel(int hdc, int x, int y);

        [DllImport("user32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
        private static extern int ReleaseDC(int hwnd, int hdc);

        [DllImport("user32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
        private static extern int WindowFromPoint(int x, int y);

        [DllImport("user32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
        private static extern int ScreenToClient(int hwnd, ref Point lppoint);

        public static Color GetScreenPointColor(Point pt)
        {
            IntPtr dC = Colors.GetDC(new IntPtr(0));
            int pixel = Colors.GetPixel(dC, pt);
            Colors.ReleaseDC(IntPtr.Zero.ToInt32(), dC.ToInt32());
            return Color.FromArgb(pixel & 0xFF, (pixel & 0xFF00) >> 8, (pixel & 0xFF0000) >> 16);
        }

        public static Color GetScreenPointColor(int x, int y)
        {
            int hwnd = Colors.WindowFromPoint(x, y);
            int dC = Colors.GetDC(hwnd);
            int pixel = Colors.GetPixel(dC, x, y);
            return Color.FromArgb(255, Color.FromArgb(pixel));
        }

        public static Color ColorChangedLightColour(Color color, float correctionFactor)
        {
            float num = (float)(int)color.R;
            float num2 = (float)(int)color.G;
            float num3 = (float)(int)color.B;
            if (correctionFactor < 0f)
            {
                correctionFactor = 1f + correctionFactor;
                num *= correctionFactor;
                num2 *= correctionFactor;
                num3 *= correctionFactor;
            }
            else
            {
                num = (255f - num) * correctionFactor + num;
                num2 = (255f - num2) * correctionFactor + num2;
                num3 = (255f - num3) * correctionFactor + num3;
            }
            if (num < 0f)
            {
                num = 0f;
            }
            if (num > 255f)
            {
                num = 255f;
            }
            if (num2 < 0f)
            {
                num2 = 0f;
            }
            if (num2 > 255f)
            {
                num2 = 255f;
            }
            if (num3 < 0f)
            {
                num3 = 0f;
            }
            if (num3 > 255f)
            {
                num3 = 255f;
            }
            return Color.FromArgb(color.A, (int)num, (int)num2, (int)num3);
        }
    }
}
